/**
 * 列表转树
 */
export default function list2Tree(list) {
	if (!Array.isArray(list) || !list.length) {
		return []
	}

	const map = {}
	list.forEach((item) => {
		item.children = []
		map[item.id] = item
	})

	const result = []
	list.forEach((item) => {
		const parent = map[item.parentId]

		if (parent) {
			parent.children.push(item)
		} else {
			result.push(item)
		}
	})

	return result
}
